package com.foxzzz.temporary;

public class Solution6 {
    public String convert(String s, int numRows) {
        if(numRows<=1) return s;
        if(s.equals("")) return "";

        int num=numRows*2-2;
        int weight=s.length()/num*2+2;
        char[][] chars=new char[numRows][weight];
        for (int i = 0; i < s.length(); i++) {
           int indexInPreiod =i%num;
           int w=i/num*2;
           int h=0;
           if(indexInPreiod<numRows){
               h=indexInPreiod;
               chars[h][w]=s.charAt(i);
           }else {
               h=num-indexInPreiod;
               chars[h][w+1]=s.charAt(i);
           }

        }
        StringBuilder sb=new StringBuilder();
        for (int i = 0; i <numRows*weight; i++) {
            int w=i%weight;
            int h=i/weight;
            if(chars[h][w]!='\0')
            sb.append(chars[h][w]);
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        System.out.println(new Solution6().convert("PAYPALISHIRING",3));
    }
}
